<template>
  <a-modal
    v-model:open="open"
    title="随访信息"
    :footer="null"
    :maskClosable="false"
    :width="1200"
    :bodyStyle="{ height: '600px', overflow: 'hidden auto', padding: '0px 24px 20px' }"
  >
    <template v-if="formData">
      <tform :formData="formData" :disabled="true" />
    </template>
    <template v-else>
      <div style="display: flex; align-items: center; justify-content: center; height: 100%">
        <a-empty />
      </div>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import tform from '@/components/FollowForm/index.vue'
import { ref, reactive, onMounted } from 'vue'
import { getQuestionnaireInfo, getQuestionnaireAnswer } from '@/api/patientMana/index'

const open = ref(false)
const formData = ref()

const init = (data) => {
  open.value = true

  const contenData = data.itemContent
  if (contenData) {
    if (typeof contenData === 'string') {
      try {
        const dataString = JSON.parse(contenData)

        if (typeof dataString === 'object') {
          if (dataString.questionnaireRecordId) {
            getQuestionnaireAnswer({
              questionnaireRecordId: dataString.questionnaireRecordId,
              empi: data.empi
            }).then((res) => {
              if (res.data) {
                const questionData = JSON.parse(res.data.questionnaireInfoVO.content)
                const patientQuestionnaireAnswerVO = res.data.patientQuestionnaireAnswerVO

                questionData.forEach((v) => {
                  v.list.forEach((u) => {
                    const item = patientQuestionnaireAnswerVO.filter((w) => {
                      return w.questionId === u.id
                    })

                    if (item && item.length === 1) {
                      u.value = item[0].content
                      u.inputText = item[0].content
                    } else if (item && item.length > 1) {
                      const checkData: any = []
                      item.forEach((w) => {
                        checkData.push(w.content)
                      })
                      u.value = checkData.join(',')
                      u.inputText = u.value
                    }
                    u.exc = u.exc ? JSON.parse(u.exc) : {}
                  })
                })
                formData.value = questionData
              } else {
                getinitFormData(dataString.questionnaireId)
              }
            })
          } else {
            if (dataString.questionnaireId) {
              getinitFormData(dataString.questionnaireId)
            }
          }
        } else {
          formData.value = null
        }
      } catch (e) {
        formData.value = null
      }
    }
  } else {
    formData.value = null
  }
}

const getinitFormData = (id) => {
  getQuestionnaireInfo({ questionnaireId: id }).then((res) => {
    console.log(res)
    if (res.data && res.data.content) {
      //
      formData.value = JSON.parse(res.data.content)
    }
  })
}

defineExpose({
  init
})
</script>
<style lang="less" scoped></style>
